package controlSoftware.DataIn;

import java.io.IOException;
import java.util.Queue;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import controlSoftware.userThread.SensorData;
import controlSoftware.userThread.SysModel;
import controlSoftware.MockDataIn.DataSourceType;
import controlSoftware.MockDataIn.MockDataSource;

public class DataIn implements Runnable {
	private static Logger LOGGER;
	String threadName;

	final static int SLEEPTIME = (int) SysModel.sampleTime; // 10 millisec in between data retrieval calls
	Queue<SensorData> userData = null;

	SensorData data = null;

	public DataIn(String threadName, Queue<SensorData> userData) {
		this.userData = userData;
		this.threadName = threadName;
	}
	
	public String getName(){
		return threadName;
	}

	@Override
	public void run() {
		try{
		FileHandler handler = new FileHandler("log/rawData.log", false);
		LOGGER = Logger.getLogger(DataIn.class.getName());
		LOGGER.addHandler(handler);
		}
		catch(IOException e){
			System.out.println("Raw Data Log Generation Error!\n");
		}
		
		//declare mock data source: static or from file
		//MockDataSource mockDataSource = new MockDataSource(DataSourceType.STATIC);
		MockDataSource mockDataSource = new MockDataSource(DataSourceType.FILE);

		while (true) {
			//test with mock data
			data = mockDataSource.getNextMockData();

			if (data != null) {
				userData.add(data);
				
				// log data
				LOGGER.info( this.getName() + " active: \n"
						+ "x: " + data.accel_x
						+ " y: " + data.accel_y 
						+ " z: " + data.accel_z
						+ "\npitch: " + data.pitch 
						+ " roll: " + data.roll 
						+ " yaw: " + data.yaw );
			}

			try {
				Thread.sleep(SLEEPTIME);
			} catch (InterruptedException e) {
				LOGGER.severe("DataIn Thread interrupted.");
			}
		}
	}

}
